﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Canyala.Lagoon.Models
{
    public class TableEntityKey
    {
        public readonly string PartitionKey;
        public readonly string RowKey;

        public TableEntityKey(string partitionKey, string rowKey)
        {
            PartitionKey = partitionKey;
            RowKey = rowKey;
        }

        public static TableEntityKey Empty = new TableEntityKey(String.Empty, String.Empty);

        public static TableEntityKey FromPartitionAndRowKeys(string partitionKey, string rowKey)
            { return new TableEntityKey(partitionKey, rowKey); }

        public static TableEntityKey Parse(string text)
        {
            var parts = text.Split('@');
            return FromPartitionAndRowKeys(parts[1], parts[0]);
        }

        public override string ToString()
            { return String.Concat(RowKey, "@", PartitionKey); }

        public override int GetHashCode()
            { return ToString().GetHashCode(); }

        public override bool Equals(object obj)
        {
            var key = obj as TableEntityKey;
            if (key == null) return false;

            if (PartitionKey != key.PartitionKey)
                return false;

            if (RowKey != key.RowKey)
                return false;

            return true;
        }
    }
}
